/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { Mustache } from './mustache/mustache.js'

const renderList = [
  "NAME:{{name}}",
  "EMPTY:{{nothing}}",
  "{{focus}}",
  "{{{focus}}}",
  "{{&focus}}",
  "SEX:{{msg.sex}};AGE:{{msg.age}}",
  "{{#msg}}SEX:{{sex}};AGE:{{age}};HOBBY:{{hobby}}{{/msg}}",
  "{{#subject}}{{.}}{{/subject}}",
  "EMPTY-ARR:{{#moreInfo}}empty-arr{{/moreInfo}}\nARRAY: {{#subject}}arr {{/subject}}\n",
  "EMPTY-ARR:{{^moreInfo}}empty-arr{{/moreInfo}}\nARRAY: {{^subject}}arr {{/subject}}\n",
  "{{!name}}.",
  "NAME:{{name}}\nINFO:\n{{>template}}",
  "NAME:{{name}}\nNAME:<%name%>\n",
  "NAME:{{name}}\nNAME:<%name%>\n"
]

const data = {
  name: "cai",
  msg: {
    sex: "male",
    age: "66",
    hobby: "reading"
  },
  focus: "<span>sleep<span>",
  subject: [" Ch ", " En ", " Math ", " physics "],
  moreInfo: []
}


function render_Test_01() {
  let result = 'NAME:cai'
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    renderStr = Mustache.render(renderList[0], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  print(`Jmustache_render(parse): ${endTime - startTime} ms`)
}


function render_Test_02() {
  let result = 'EMPTY:'
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    renderStr = Mustache.render(renderList[1], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  print(`Jmustache_render(empty): ${endTime - startTime} ms`)
}


function render_Test_03() {
  let result = '&lt;span&gt;sleep&lt;span&gt;'
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    renderStr = Mustache.render(renderList[2], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  print(`Jmustache_render(HTML): ${endTime - startTime} ms`)
}


function render_Test_04() {
  let result = '<span>sleep<span>'
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    renderStr = Mustache.render(renderList[3], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  print(`Jmustache_render(noHTML): ${endTime - startTime} ms`)
}


function render_Test_05() {
  let result = '<span>sleep<span>'
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    renderStr = Mustache.render(renderList[4], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  print(`Jmustache_render(noEscap): ${endTime - startTime} ms`)
}


function render_Test_06() {
  let result = 'SEX:male;AGE:66'
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    renderStr = Mustache.render(renderList[5], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  print(`Jmustache_render(object): ${endTime - startTime} ms`)
}


function render_Test_07() {
  let result = 'SEX:male;AGE:66;HOBBY:reading'
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    renderStr = Mustache.render(renderList[6], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  print(`Jmustache_render(block): ${endTime - startTime} ms`)
}


function render_Test_08() {
  let result = ' Ch  En  Math  physics '
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    renderStr = Mustache.render(renderList[7], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  print(`Jmustache_render(enum): ${endTime - startTime} ms`)
}


function render_Test_09() {
  let result = `EMPTY-ARR:
ARRAY: arr arr arr arr 
`
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    renderStr = Mustache.render(renderList[8], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  print(`Jmustache_render(if): ${endTime - startTime} ms`)
}


function render_Test_10() {
  let result = `EMPTY-ARR:empty-arr
ARRAY: 
`
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    renderStr = Mustache.render(renderList[9], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  print(`Jmustache_render(else): ${endTime - startTime} ms`)
}


function render_Test_11() {
  let result = '.'
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    renderStr = Mustache.render(renderList[10], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  print(`Jmustache_render(node): ${endTime - startTime} ms`)
}


function render_Test_12() {
  let result = `NAME:cai
INFO:
SEX:male;AGE:66;HOBBY:reading`
  let partials = { template: "{{#msg}}SEX:{{sex}};AGE:{{age}};HOBBY:{{hobby}}{{/msg}}" };
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    renderStr = Mustache.render(renderList[11], data, partials)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  print(`Jmustache_render(module): ${endTime - startTime} ms`)
}


function render_Test_13() {
  let result = `NAME:{{name}}
NAME:cai
`
  let customTags = ['<%', '%>'];
  let obj = {};
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    renderStr = Mustache.render(renderList[12], data, obj, customTags)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  print(`Jmustache_render(customTags): ${endTime - startTime} ms`)
}


function parse_Test() {
  let result = `NAME:cai
NAME:<%name%>
`
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
    Mustache.parse(renderList[13])
  }
  let endTime = Date.now();
  renderStr = Mustache.render(renderList[13], data)
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  print(`Jmustache_parse: ${endTime - startTime} ms`)
}


function clearCache_Test() {
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    Mustache.clearCache()
  }
  let endTime = Date.now();
  print(`Jmustache_clearCache: ${endTime - startTime} ms`)
}

function mustacheTest() {
  render_Test_01();
  render_Test_02();
  render_Test_03();
  render_Test_04();
  render_Test_05();
  render_Test_06();
  render_Test_07();
  render_Test_08();
  render_Test_09();
  render_Test_10();
  render_Test_11();
  render_Test_12();
  render_Test_13();
  parse_Test();
  clearCache_Test();
}

let loopCountForPreheat = 1;
for (let i = 0; i < loopCountForPreheat; i++) {
  mustacheTest();
}

ArkTools.waitAllJitCompileFinish();

mustacheTest();